package cn.edu.hunan.pm.advice;

import cn.edu.hunan.pm.common.R;
import cn.edu.hunan.pm.exception.BusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一的异常处理类
 * @author hb
 * @date 2024-06-20 10:22:54
 */
@ControllerAdvice  //全局处理异常的切面
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e){
        return R.error().code(400444).message("未知错误");
    }


    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public R error(BusinessException e){
        return R.error().code(e.getCode()).message(e.getMessage());
   }


}
